//
//  MCNetTool.m
//  zxc
//
//  Created by 李李善 on 2018/2/7.
//  Copyright © 2018年 李李善. All rights reserved.
//

#import "MCNetTool.h"

@implementation MCNetTool
+ (instancetype)sharedTool
{
    static MCNetTool *tool = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSURL *baseURL = [NSURL URLWithString:host];
        tool = [[self alloc] initWithBaseURL:baseURL];
     
        //神坑
        
        [tool.requestSerializer setValue:@"application/json;charset=utf-8;Content-Length" forHTTPHeaderField:@"Content-Type"];
       
        //[tool.requestSerializer setValue:@"application/x-www-form-urlencoded;charset=utf-8;application/json" forHTTPHeaderField:@"Content-Type"];
        
        
//        [tool.requestSerializer setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-type"];
        
        
//        [tool.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
      

//        [tool.requestSerializer setValue:@"text/html" forHTTPHeaderField:@"Content-Type"];  // 此处设置content-Type生效了，然后就是参数要求是json，怎么设。。

        
        
        //这里有个问题,如果要设置请求登陆时不保存cookies,这里可以在AFHTTPSessionManager设置
        //[netWork.requestSerializer setHTTPShouldHandleCookies:NO];
        //来实现.
        //netWork.requestSerializer.HTTPShouldHandleCookies = YES;
        
        
        
        
        AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
        // 如果是需要服务端验证证书，需要设置为YES
        securityPolicy.allowInvalidCertificates = YES;
        //validatesDomainName 是否需要验证域名，默认为YES；
        securityPolicy.validatesDomainName = NO;
        //设置验证模式
        tool.securityPolicy = securityPolicy;
        
        
        //        Content-Type = "text/html; charset=utf-8",
       // tool.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/javascript",@"text/xml", nil];

                tool.responseSerializer.acceptableContentTypes = [NSSet setWithArray:@[
                                                                                  @"application/json",
                                                                                  @"text/html",
                                                                                  @"text/json",
                                                                                  @"text/plain",
                                                                                  @"text/javascript",
                                                                                  //@"text/xml",
                                                                                  //@"image/*"
                                                                                  ]];
        
        
    
        //???: 测试添加
        //        ((AFJSONResponseSerializer *)netWork.responseSerializer).readingOptions = NSJSONReadingAllowFragments | NSJSONReadingMutableLeaves | NSJSONReadingMutableContainers;
        
        
        //请求方式
        tool.requestType = kWZXRequestTypeJSON;
        
        //响应
        tool.responseType = kWZXResponseTypeData;//kWZXResponseTypeJSON;//
        
        // 开始对网络检测
        [tool.reachabilityManager startMonitoring];
        
    });
    return tool;
}

/*
 1:父类初始化方法（AFHTTPSessionManager）
 2:timeoutInterval 超时时间
 */
- (instancetype)initWithBaseURL:(NSURL *)url
{
    self = [super initWithBaseURL:url];
    if (self) {
        //  netWork.requestType  = kWZXRequestTypePlainText;
        
        //netWork.responseType = kWZXResponseTypeJSON;
        
        //设置网络请求超时时间
        self.timeout = 10.f;
        
        // 设置允许同时最大并发数量，过大容易出问题
        self.operationQueue.maxConcurrentOperationCount = 3;
        self.reachabilityManager =  [AFNetworkReachabilityManager sharedManager];
    }
    return self;
}



/**
 AFXMLParserResponseSerializer      XML,只能返回XMLParser,还需要自己通过代理方法解析
 AFXMLDocumentResponseSerializer (Mac OS X)
 AFPropertyListResponseSerializer   PList
 AFImageResponseSerializer          Image
 AFCompoundResponseSerializer       组合
 */
-(void)setResponseType:(WZXResponseType)responseType
{
   
    if (_responseType != responseType) {
        switch (responseType) {
            case kWZXResponseTypeJSON:{
                self.responseSerializer = [AFJSONResponseSerializer serializer];
            }
                break;
            case kWZXResponseTypeXML:{
                self.responseSerializer = [AFXMLParserResponseSerializer serializer];
            }
                break;
            case kWZXResponseTypeData:{
                //如果不是json则用本语句 // 设置响应解析数据的类型@"text/html",
                self.responseSerializer = [AFHTTPResponseSerializer serializer];
            }
                break;
        }
        _responseType = responseType;
    }
}


-(void)setRequestType:(WZXRequestType)requestType
{
    if (_requestType != requestType) {
        
        switch (requestType) {
            case kWZXRequestTypeJSON:{
                //json必备
                self.requestSerializer = [AFJSONRequestSerializer serializer];
                
            }
                break;
            case kWZXRequestTypePlainText:{
                self.requestSerializer = [AFHTTPRequestSerializer serializer];
                
            }
                break;
            case kWZXRequestTypePropertyList:
            {
                self.requestSerializer = [AFPropertyListRequestSerializer serializer];
            }
                break;
        }
        _requestType = requestType;
    }
}


-(void)setTimeout:(NSTimeInterval)timeout
{
    if (_timeout!=timeout) {
        self.requestSerializer.timeoutInterval=timeout;
        _timeout =timeout;
    }
}







@end
